package es.timmps.luchynews.adapter;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;



import es.timmps.luchynews.R;
import es.timmps.luchynews.daos.*;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NoticiaItem extends BaseAdapter {

	ArrayList<Noticia> lista;
	private Context mContext;

	
	public NoticiaItem(Context context, ArrayList<Noticia> items) {
		
		     mContext = context;
		
		     lista = items;
		
		}

	
	public int getCount() {
		
		return lista.size();
	}

	public Object getItem(int arg0) {
		
		return lista.get(arg0);
	}

	public long getItemId(int arg0) {
		
		return arg0;
	}

	public View getView(int position, View convertView, ViewGroup arg2) {
		View vi=convertView;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) 
				this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.news_item, null);
}
Noticia item = lista.get(position);
try{
ImageView image = (ImageView) vi.findViewById(R.id.minia);
image.setImageBitmap(getImg(item.getMiniatura()));

}catch(NullPointerException e){
	ImageView image = (ImageView) vi.findViewById(R.id.minia);
	image.setImageResource(R.drawable.ic_launcher);
}
		TextView titular = (TextView) vi.findViewById(R.id.titular);
titular.setText(item.getTitular());
TextView descripcion = (TextView) vi.findViewById(R.id.desc);
descripcion.setText(Html.fromHtml(item.getDescripcion()));
TextView fecha = (TextView) vi.findViewById(R.id.fecha);
fecha.setText(item.getFechaPub());
return vi;
	}
	protected Bitmap getImg(String urls) {
        String urldisplay = urls;
        Log.d("Imagen",urls);
        Bitmap mIcon11 = null;
        
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
            mIcon11 = mIcon11.createScaledBitmap(mIcon11, 90, 70, true);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

}
